<?php
/**
 * @package scuttle
 * @file /class/AbstractEditAction.class.php
 **/

if (!defined('XOOPS_ROOT_PATH')) exit();

class Scuttle_AbstractEditAction extends Scuttle_Action
{
	var $mObject = null;
	var $mObjectHandler = null;
	var $mActionForm = null;
	var $mConfig;

	/**
	 * @access protected
	 */
	function _getId()
	{
	}

	/**
	 * @access protected
	 */
	function &_getHandler()
	{
	}

	/**
	 * @access protected
	 */
	function _setupActionForm()
	{
	}

	/**
	 * @access protected
	 */
	function _setupObject()
	{
		$id = $this->_getId();
		$this->mObjectHandler = $this->_getHandler();
		$this->mObject =& $this->mObjectHandler->get($id);

		if ($this->mObject == null && $this->isEnableCreate()) {
			$this->mObject =& $this->mObjectHandler->create();
		}
	}

	/**
	 * @access protected
	 */
	function isEnableCreate()
	{
		return true;
	}

	function prepare(&$controller, &$xoopsUser, $moduleConfig)
	{
		$this->mConfig = $moduleConfig;

		$this->_setupActionForm();
		$this->_setupObject();
	}

	function getDefaultView(&$controller, &$xoopsUser)
	{

		// IuWFNg?AG[?

		if ($this->mObject == null) {
			return SC_FRAME_VIEW_ERROR;
		}
		// ANVtH[ load s
		$this->mActionForm->load($this->mObject);
		return SC_FRAME_VIEW_INPUT;
	}

	function execute(&$controller, &$xoopsUser)
	{
		// IuWFNg?AG[?
		if ($this->mObject == null) {
			return SC_FRAME_VIEW_ERROR;
		}

		// tH[?LZ?
		if (xoops_getrequest('_form_control_cancel') != null) {
			return SC_FRAME_VIEW_CANCEL;
		}
		// ANVtH[ load s
		$this->mActionForm->load($this->mObject);
		// ANVtH[ fetch s
		$this->mActionForm->fetch();
		// ?`FbN
		$this->mActionForm->validate();
		if ($this->mActionForm->hasError()) {
			// G[
			return SC_FRAME_VIEW_INPUT;
		}
		// f[^?XV
		$this->mActionForm->update($this->mObject);

		return $this->_doExecute() ? SC_FRAME_VIEW_SUCCESS :
		                             SC_FRAME_VIEW_ERROR ;
	}

	/**
	 * @access protected
	 */
	function _doExecute()
	{
		return $this->mObjectHandler->insert($this->mObject);
	}
}

?>